package com.edu.koTA07.utils;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.edu.koTA07.XmlDataSet.SalesXmlDataSet;
import com.edu.koTA07.sqlHelper.SQLHelper;

public class SalesUtils {
	SQLHelper helper;
	SQLiteDatabase database;	
	
	public SalesUtils(Context ctx) {
		helper = new SQLHelper(ctx);
		database = helper.getWritableDatabase();
	}
	
	@SuppressWarnings("static-access")
	public ArrayList<SalesXmlDataSet> loadData() {
		ArrayList<SalesXmlDataSet> currentData = new ArrayList<SalesXmlDataSet>();
		Cursor dataCursor = database.query(helper.TABLE_SALES, 
				new String[] {helper.ID_SALES, helper.ID_PERUSAHAAN1, helper.NAMA_SALES, 
				helper.USERNAME, helper.PASSWORD}, 
				null, null, null, null, null);
//				String.format("%s, %s", helper.TASK_COMPLETE2, helper.TASK_NAME2));
		
		dataCursor.moveToFirst(); SalesXmlDataSet t;		
		if ( !dataCursor.isAfterLast() ) {
			do {
				String idSales = dataCursor.getString(0);
				String idPerusahaan = dataCursor.getString(0); 
				String namaSales = dataCursor.getString(2);
				String username = dataCursor.getString(3); 
				String password = dataCursor.getString(4);
//				boolean complete = Boolean.parseBoolean(boolValue);				
				
				t = new SalesXmlDataSet();
				t.setIDSales(idSales);
				t.setIDPerusahaan(idPerusahaan);
				t.setNamaSales(namaSales);
				t.setUsername(username);
				t.setPassword(password);
				currentData.add(t);
				
			} while(dataCursor.moveToNext());
		}		
		dataCursor.close();		
		return currentData;
	}
	
	@SuppressWarnings("static-access")
	public void onSaveDataFromXml(SalesXmlDataSet getSales) {
    	//assert (null != getSales);
    	
		for (int i = 0; i < getSales.getNamaSales().size(); i++) {
			ContentValues values = new ContentValues();
			values.put(helper.ID_SALES, getSales.getIDSales().get(i));
			values.put(helper.ID_PERUSAHAAN1, getSales.getIDPerusahaan().get(i));
			values.put(helper.NAMA_SALES, getSales.getNamaSales().get(i));
			values.put(helper.USERNAME, getSales.getUsername().get(i));
			values.put(helper.PASSWORD, getSales.getPassword().get(i));
			database.insert(helper.TABLE_SALES, null, values);
    	}
		
    }
    
//    @SuppressWarnings("static-access")
//	public void onUpdateData(Hutang getCust) {
//    	assert (null != getCust);    	
//    	ContentValues values = new ContentValues();
//		values.put(helper.TASK_NAME2, getCust.getName());
//		values.put(helper.TASK_ADDRESS2, getCust.getAddress());
//		values.put(helper.TASK_PHONE2, getCust.getPhone());
//		values.put(helper.TASK_COMPLETE2, Boolean.toString(getCust.isComplete()));		
//		long id = getCust.getId();
//		String where = String.format("%s = %d", helper.TASK_ID2, id);
//		database.update(helper.TASKS_TABLE2, values, where, null);
//    }    
//    AlertDialog alert;
//    public void onShowData(Hutang cust, Context ctx) {
//		final Hutang thisCust = cust;		
//		alert = new AlertDialog.Builder(ctx).setIcon(R.drawable.icon)
//	        .setTitle("Display Data")
//	        .setMessage(" ------------ Hutang -------------\n"
//	        		  + "ID: "+thisCust.getId()+"\n" 
//	        		  + "Name: "+thisCust.getName()+"\n"
//	        		  + "Adress: "+thisCust.getAddress()+"\n"
//	        		  + "Phone: "+thisCust.getPhone()+"\n")
//	        .setNegativeButton("Close", new DialogInterface.OnClickListener() {
//	            public void onClick(DialogInterface dialog, int whichButton) {
//	               alert.cancel();
//	            }
//	        }).create();			
//		alert.show();

}


